package com.jala.demo;

import com.jala.base.BaseAction;
import com.jala.cm.Paginator;
import com.jala.cm.Result;
import com.jala.demo.api.DemoApiService;
import com.jala.demo.model.DemoVO;
import com.jala.demo.req.AddDemoReq;
import com.jala.demo.req.DelDemoReq;
import com.jala.demo.req.EditDemoReq;
import com.jala.demo.req.PageDemoReq;
import com.jala.demo.resp.AddDemoResp;
import com.jala.demo.resp.DelDemoResp;
import com.jala.demo.resp.EditDemoResp;
import com.jala.demo.resp.PageDemoResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/3/15.
 */
@Controller
@RequestMapping("/demo")
public class DemoAction extends BaseAction{

    @Autowired
    private DemoApiService demoApiService;

    @RequestMapping("/pageDemoList.htm")
    public String pageUserList(ModelMap model, Paginator page, @ModelAttribute DemoVO demoVO) {
        logger.info("********pageUserList.htm**********");

        //获取传入值
        PageDemoReq req = new PageDemoReq();
        if (StringUtils.isNotEmpty(demoVO.getName())) {
            demoVO.setName(demoVO.getName().trim());
        }
        req.setName(demoVO.getName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageDemoResp resp = demoApiService.pageDemoList(req);
        List<DemoVO> demoVOS = resp.getDemoVOS();
        page.setTotalCount(resp.getTotalCount());
        model.put("paginator", page);
        model.put("demoVOS", demoVOS);
        model.put("demoVO", demoVO);
        return "demo/list";
    }

    @RequestMapping("/addDemo.json")
    @ResponseBody
    public Result addDemo(@ModelAttribute DemoVO demoVO) {
        Result result =new Result();
        logger.warn("********addDemo.json**********");
        AddDemoReq req = new AddDemoReq();
        req.setDemoVO(demoVO);
        AddDemoResp resp = demoApiService.addDemo(req);
        result.setMsg(resp.getMsg());
        result.setCode(resp.getCode());
        return result;
    }


    @RequestMapping("/editDemo.json")
    @ResponseBody
    public Result editDemo(@ModelAttribute DemoVO demoVO) {
        Result result =new Result();
        logger.warn("********addDemo.json**********");
        EditDemoReq req = new EditDemoReq();
        req.setDemoVO(demoVO);
        EditDemoResp resp = demoApiService.editDemo(req);
        result.setMsg(resp.getMsg());
        result.setCode(resp.getCode());
        return result;
    }

    @RequestMapping("/deleteDemo.json")
    @ResponseBody
    public Result deleteDemo(@ModelAttribute DemoVO demoVO) {
        Result result =new Result();
        logger.warn("********deleteDemo.json**********");
        DelDemoReq req = new DelDemoReq();
        req.setId(demoVO.getId());
        DelDemoResp resp = demoApiService.deleteDemo(req);
        result.setMsg(resp.getMsg());
        result.setCode(resp.getCode());
        return result;
    }



    @RequestMapping("/demoDetail.htm")
    public String demoDetail(ModelMap model, Paginator page, @ModelAttribute DemoVO demoVO) {
        //List strs = List.of("Hello", "World");
        logger.debug("********demoDetail.htm**********");
        PageDemoReq req = new PageDemoReq();
        req.setId(demoVO.getId());
        req.setName(demoVO.getName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageDemoResp resp = demoApiService.pageDemoList(req);
        List<DemoVO> demoVOS = resp.getDemoVOS();
        page.setTotalCount(resp.getTotalCount());
        model.put("paginator", page);
        model.put("demoVOS", demoVOS);
        model.put("demoVO", demoVO);
        return "demo/detail";
    }

    @RequestMapping("/ajaxDemoInfo.json")
    @ResponseBody
    public List<DemoVO> ajaxDemoInfo(Paginator page, @ModelAttribute DemoVO demoVO) {
        logger.info("********ajaxDemoInfo.json**********");
        PageDemoReq req = new PageDemoReq();
        req.setName(demoVO.getName());
        req.setPageNo(page.getPageNum());
        req.setPageSize(page.getPageSize());
        PageDemoResp resp = demoApiService.pageDemoList(req);
        List<DemoVO> demoVOS = resp.getDemoVOS();
        return demoVOS;
    }


}
